#include "Drawing.hpp"

namespace zen {

	namespace video {


		const glm::float32 Drawing::DEFAULT_DEPTH = 0.0f;
		glm::u8vec4 Drawing::resColor = glm::u8vec4(255, 255, 255, 255);
		glm::u8vec4 Drawing::color = glm::u8vec4(255, 255, 255, 255);
		glm::u8vec4 Drawing::backColor = glm::u8vec4(0, 0, 0, 255);
		

		void Drawing::drawRectangle(float x, float y, float width, float height, bool fill, float depth) {
			if (fill) {
				glBegin(GL_QUADS);
					activateBackgroundColor();
					glVertex3f(x, y - 1, depth);
					glVertex3f(x, y + height - 1, depth);
					glVertex3f(x + width, y + height - 1, depth);
					glVertex3f(x + width, y, depth);
					glVertex3f(x, y - 1, depth);
				glEnd();
			}
			glBegin(GL_LINE_STRIP);
				activateColor();
				glVertex3f(x, y, depth);
				glVertex3f(x, y + height - 1, depth);
				glVertex3f(x + width - 1, y + height - 1, depth);
				glVertex3f(x + width - 1, y, depth);
				glVertex3f(x, y, depth);
				restoreColor();
			glEnd();
		}

		void Drawing::drawEllipse(float x, float y, float width, float height, bool fill, float depth) {
			width /= 2.0;
			height /= 2.0;
			float div = (float)(12 + (width + height) / 10);
			if (fill) {
				glBegin(GL_TRIANGLE_FAN);
					activateBackgroundColor();
					glVertex3f(x, y, depth);
					for (float i=0; i<=div; i++)
						glVertex3f(x + (float)cos(i * (_2PI / div)) * width, y + (float)sin(i * (_2PI / div)) * height, depth);
				glEnd();
			}
			glBegin(GL_LINE_STRIP);
				activateColor();
				for (float i=0; i<=div; i++)
					glVertex3f(x+(float)cos(i*(_2PI/div))*width, y+(float)sin(i*(_2PI/div))*height, depth);
				restoreColor();
			glEnd();
		}

		void Drawing::drawTexture(const PTexture &texture, float x, float y, float width, float height, float depth) {
			texture->activate();
			glBegin(GL_QUADS);
				activateColor();
				glTexCoord2f(0.0f, 1.0f);
				glVertex3f(x, y-1, depth);
				glTexCoord2f(0.0f, 0.0f);
				glVertex3f(x, y+height-1, depth);
				glTexCoord2f(1.0f, 0.0f);
				glVertex3f(x+width, y+height-1, depth);
				glTexCoord2f(1.0f, 1.0f);
				glVertex3f(x+width, y, depth);
				restoreColor();
			glEnd();
		}

		void Drawing::drawText(const PFont &font, const std::string &text, float x, float y, float depth) {
			activateColor();
			font->drawText(text, x, y, depth);
			restoreColor();
		}


	} // namespace video

} // namespace zen
